//
//  HMHomeSubViewController.m
//  HMPersonalDoctor
//
//  Created by GD on 16/9/10.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMHomeSubViewController.h"

@interface HMHomeSubViewController ()<UIWebViewDelegate>
//属性WebView
@property(weak,nonatomic)UIWebView *webView;

@end

@implementation HMHomeSubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
    [self loadData];
}

- (void)setupUI
{
    ////  添加上面左边的按钮
//    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"列表.png"] style:UIBarButtonSystemItemDone target:self action:@selector(clickBackHome)];
//    self.navigationItem.leftBarButtonItem = leftBarItem;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    //创建一个webView  是否-64
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width ,[UIScreen mainScreen].bounds.size.height-64)];
    
    // 把webView添加到视图上
    [self.view addSubview:webView];
    self.webView = webView;
    NSLog(@"%@",NSStringFromCGRect(self.webView.frame));
    //  webView的代理
    self.webView.delegate = self;
    //  webView自动对页面进行适配
    webView.scalesPageToFit = YES;
    
}


#pragma mark-UIWebViewDelegate
//可以拦截  HTML中的点击事件
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSLog(@"shouldStartLoad--   %@",request);
    
    //跳转需要有返回按钮?  在导航栏中
    
    return YES;
}

//js调用OC  注入
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"%@",webView);
    
    NSMutableString *stringM = [NSMutableString string];
    
    [stringM appendString:@"var headerTag = document.getElementsByTagName('header')[0]; headerTag.parentNode.removeChild(headerTag);"];
    
    [webView stringByEvaluatingJavaScriptFromString:stringM];
}

-(void)setUrl:(NSString *)url{//为什么该方法不起作用?  难道不可以通过url来复用吗?
    _url = url;
    //  创建URL
    NSURL *URL = [NSURL URLWithString:url];
    //  创建并发送请求
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    //   加载数据
    [self.webView loadRequest:request];
    
}

-(void)setSubTitle:(NSString *)subTitle{
    _subTitle = subTitle;
    
    self.title = subTitle;
}


-(void)loadData{
    //  创建URL
    NSURL *URL = [NSURL URLWithString:self.url];
    //  创建并发送请求
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    //   加载数据
    [self.webView loadRequest:request];
}

@end
